package com.ruoyi.coal.modules.coalpile.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.CoalPileInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardHistoryEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalPileInfoVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardHistoryVo;
import com.ruoyi.coal.modules.coalpile.service.CoalPileInfoService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardHistoryService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.utils.compute.Compute;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.NoResponseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xmlpull.v1.XmlPullParserException;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "coalyardhistory", tags = {"煤场历史数据信息"})
@RestController
@RequestMapping({"coalpile/coalyardhistory"})
@DataSource(value = DataSourceType.SLAVE)
public class CoalYardHistoryController {
  @Autowired
  private CoalYardHistoryService coalYardHistoryService;

  @Autowired
  private CoalYardInfoService coalYardInfoService;

  @Autowired
  private CoalPileInfoService coalPileInfoService;

  @ApiOperation(value = "分页查询", notes = "分页查询")
  @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc,desc)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "key", value = "关键字", paramType = "query", dataType = "String")})
  @GetMapping({"/list"})
  @RequiresPermissions({"coalpile:coalyardhistory:list"})
  public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
    PageUtils page = this.coalYardHistoryService.queryPage(params);
    for (Object o : page.getList()) {
      CoalYardHistoryEntity coalYardHistoryEntity = (CoalYardHistoryEntity)o;
      CoalYardInfoEntity byId = (CoalYardInfoEntity)this.coalYardInfoService.getById(coalYardHistoryEntity.getCoalYardId());
      List<CoalPileInfoVo> pdfVo1 = this.coalPileInfoService.getALLByHistoryId(coalYardHistoryEntity.getId());
      Map<String, Double> stringDoubleMap = Compute.computeWholeCoalpile(byId.getName(), pdfVo1);
      coalYardHistoryEntity.setVolume(stringDoubleMap.get("sumVolume"));
    }
    return R.ok().put("page", page);
  }

  @ApiOperation(value = "获取所有煤场信息和稀疏pcd路径", notes = "获取所以煤场信息和稀疏pcd路径")
  @GetMapping({"/getNew/{coalYardId}"})
  public R getNew(@PathVariable("coalYardId") Integer coalYardId) {
    CoalYardHistoryVo coalYardHistory = this.coalYardHistoryService.getAllNew(coalYardId);
    return R.ok().put("data", coalYardHistory);
  }

  @ApiOperation(value = "获取所有煤场信息和稀疏pcd路径", notes = "获取所有煤场信息和稀疏pcd路径")
  @GetMapping({"/getNewall/{coalYardId}"})
  public R getNewAll(@PathVariable("coalYardId") Integer coalYardId) {
    CoalYardHistoryVo coalYardHistory = this.coalYardHistoryService.getAllNew(coalYardId);
    if (coalYardHistory == null)
      return R.error("没有找到数据");
    coalYardHistory.setUrl(coalYardHistory.getUrl().split("\\.pcd")[0] + "_all.pcd");
    return R.ok().put("data", coalYardHistory);
  }

  @ApiOperation(value = "获取煤堆id,name,体积", notes = "获取煤堆id,name,体积")
  @GetMapping({"/getCoalPile"})
  public R getCoalPile(@RequestParam("coalYardHistoryId") Integer coalYardHistoryId) {
    List<CoalPileInfoVo> allByHistoryId = this.coalPileInfoService.getALLByHistoryId(coalYardHistoryId);
    return R.ok().put("data", allByHistoryId);
  }

  @ApiOperation(value = "修改体积", notes = "修改体积")
  @GetMapping({"/fixVolume"})
  public R getNewAll(@RequestParam("coalYardHistoryId") Integer coalYardHistoryId, @RequestParam("coalPileId") Integer coalPileId, @RequestParam("volume") Double volume) {
    CoalYardHistoryEntity coalYardHistoryEntity = (CoalYardHistoryEntity)this.coalYardHistoryService.getById(coalYardHistoryId);
    CoalYardInfoEntity coalYardInfoEntity = (CoalYardInfoEntity)this.coalYardInfoService.getById(coalYardHistoryEntity.getCoalYardId());
    CoalPileInfoEntity byId = (CoalPileInfoEntity)this.coalPileInfoService.getById(coalPileId);
    Double add_volume = Double.valueOf(volume.doubleValue() - byId.getVolume().doubleValue());
    Double add_weight = Double.valueOf(add_volume.doubleValue() * byId.getDensity().doubleValue());
    byId.setVolume(volume);
    byId.setWeight(Double.valueOf(volume.doubleValue() * byId.getDensity().doubleValue()));
    this.coalPileInfoService.updateById(byId);
    if (!byId.getName().equals(coalYardInfoEntity.getName())) {
      List<CoalPileInfoVo> allByHistoryId = this.coalPileInfoService.getALLByHistoryId(coalYardHistoryId);
      if (allByHistoryId != null && allByHistoryId.size() > 1) {
        CoalPileInfoEntity name = (CoalPileInfoEntity)this.coalPileInfoService.getOne((Wrapper)((QueryWrapper)(new QueryWrapper()).eq("name", coalYardInfoEntity.getName())).eq("history_id", coalYardHistoryId));
        if (name != null) {
          name.setWeight(Double.valueOf(name.getWeight().doubleValue() + add_weight.doubleValue()));
          name.setVolume(Double.valueOf(name.getVolume().doubleValue() + add_volume.doubleValue()));
          this.coalPileInfoService.updateById(name);
        }
      }
    }
    List<CoalPileInfoVo> newAllByHistoryId = this.coalPileInfoService.getALLByHistoryId(coalYardHistoryId);
    return R.ok().put("data", newAllByHistoryId);
  }

  @ApiOperation(value = "通过id查询", notes = "通过id查询")
  @GetMapping({"/info/{id}"})
  @RequiresPermissions({"coalpile:coalyardhistory:info"})
  public R info(@PathVariable("id") Integer id) {
    CoalYardHistoryEntity coalYardHistory = (CoalYardHistoryEntity)this.coalYardHistoryService.getById(id);
    return R.ok().put("coalYardHistory", coalYardHistory);
  }

  @ApiOperation(value = "新增煤场历史数据信息", notes = "新增煤场历史数据信息")
  @PostMapping({"/save"})
  @RequiresPermissions({"coalpile:coalyardhistory:save"})
  public R save(@RequestBody CoalYardHistoryEntity coalYardHistory) {
    this.coalYardHistoryService.save(coalYardHistory);
    return R.ok();
  }

  @ApiOperation(value = "根据id修改煤场历史数据信息", notes = "根据id修改煤场历史数据信息")
  @PostMapping({"/update"})
  @RequiresPermissions({"coalpile:coalyardhistory:update"})
  public R update(@RequestBody CoalYardHistoryEntity coalYardHistory) {
    this.coalYardHistoryService.updateById(coalYardHistory);
    return R.ok();
  }

  @ApiOperation(value = "通过id删除煤场历史数据信息", notes = "通过id删除煤场历史数据信息")
  @PostMapping({"/delete"})
  @RequiresPermissions({"coalpile:coalyardhistory:delete"})
  public R delete(@RequestBody Integer[] ids) {
    String message = this.coalYardHistoryService.deleteHistoryData(ids);
    if (message.equals("删除成功"))
      return R.ok(message);
    return R.error(message);
  }

  @ApiOperation(value = "删除数据", notes = "删除数据")
  @PostMapping({"/deletePreviousUrl"})
  public R deletePreviousUrl(@RequestParam("day") long day) throws InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException, NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InternalException {
    if (day < 0L)
      return R.ok();
    String msg = this.coalYardHistoryService.deletePreviousUrl(Long.valueOf(day));
    if (StringUtils.equals(msg, "success"))
      return R.ok("删除成功");
    return R.error("删除失败");
  }
}
